package com.kinsin.kinsinlogcat

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.ComponentActivity
import com.kinsin.kinsinlogcat.databinding.ActivityMainBinding
import com.kinsin.kinsinlogsystem.EmailSender
import com.kinsin.kinsinlogsystem.FileUtils
import com.kinsin.kinsinlogsystem.FilterClickListener
import com.kinsin.kinsinlogsystem.LogServiceConfig
import com.kinsin.kinsinlogsystem.LogcatService
import com.kinsin.kinsinlogsystem.UploadLogDialog
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

class MainActivity : ComponentActivity() {

    private lateinit var _binding: ActivityMainBinding

    private var BASE_LOG_PATH = "logRollPath"
    private var BASE_ZIP_PATH = "logZipPath"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        initEmail()
        startLogService()
        initAction()
        logTest()
    }

    private fun initEmail() {
        // 初始化配置（建议在Application类中）
        EmailSender.getInstance().configure("smtp.qq.com", "587", "123456789@qq.com", "your smtp token", true)
    }

    private fun logTest() {
        MainScope().launch {
            while (true) {
                for (i in 0..100) {
                    Log.d(
                        TAG,
                        "logTest:asdsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaajfdsgggggggggggggggggggggggggggggggggggggggggggggggggggggl $i"
                    )
                }
                delay(100)
            }
        }
    }

    private fun initAction() {
        // 上传日志监听
        _binding.uploadBtn.setOnClickListener(object : FilterClickListener() {
            override fun onFilterClick(view: View?) {
                val basePath = FileUtils.getExternalFilesPath(this@MainActivity)
                if (basePath != null) {
                    Log.d(TAG, "当前使用的是外部存储路径")
                    BASE_LOG_PATH = "${basePath}logRollPath"
                    BASE_ZIP_PATH = "${basePath}logZipPath"
                } else {
                    Log.d(TAG, "当前使用的是内部存储路径")
                    BASE_LOG_PATH = "${FileUtils.getInternalFilesPath(this@MainActivity)}logRollPath"
                    BASE_ZIP_PATH = "${FileUtils.getInternalFilesPath(this@MainActivity)}logZipPath"
                }
                UploadLogDialog(
                    this@MainActivity,
                    UploadLogDialog.UploadLogParams.Builder()
                        .setTitle("测试标题")
                        .setBaseLogPath(BASE_LOG_PATH)
                        .setZipPath(BASE_ZIP_PATH)
                        .setRecipientEmail("123456789@qq.com")
                        .addCc("987654321@qq.com")
                        .setZipPartSize(40 * 1024 * 1024) // 40MB
                        .build()
                ).show()
            }
        })

        // 测试停止服务监听
        _binding.stopBtn.setOnClickListener(object : FilterClickListener() {
            override fun onFilterClick(view: View?) {
                val intent = Intent(this@MainActivity, LogcatService::class.java)
                stopService(intent)
            }
        })
    }

    private fun startLogService() {
        val basePath = FileUtils.getExternalFilesPath(this)
        if (basePath != null) {
            Log.d(TAG, "当前使用的是外部存储路径")
            BASE_LOG_PATH = "${basePath}logRollPath"
            BASE_ZIP_PATH = "${basePath}logZipPath"
        } else {
            Log.d(TAG, "当前使用的是内部存储路径")
            BASE_LOG_PATH = "${FileUtils.getInternalFilesPath(this)}logRollPath"
            BASE_ZIP_PATH = "${FileUtils.getInternalFilesPath(this)}logZipPath"
        }

        val intent = Intent(this, LogcatService::class.java)
        val logServiceConfig = LogServiceConfig.Builder()
            .setBasePath(BASE_LOG_PATH) // 日志基础路径
            .setZipPath(BASE_ZIP_PATH) // 压缩包操作路径
            .setMaxTotalSize(530 * 1024 * 1024) // 最大容量530MB
            .setStorageReleaseStandard(480 * 1024 * 1024) // 释放停止线 480MB
            .setTempFileMaxSize(10 * 1024 * 1024) // 临时文件最大容量10MB
            .setNormalWriteMode(false) // 使用映射写入
            .setStorageCheckInterval(10) // 检测间隔10分钟
            .setStorageCheckTimeUnit(TimeUnit.MINUTES) // 检测间隔时间单位为分钟
            .build()

        intent.putExtra("config", logServiceConfig)
        startService(intent)
    }


    companion object {
        const val TAG = "MainActivity"
    }

}
